﻿Public Class GameDevelopmentManagement
    Inherits System.Web.UI.Page

    Dim objDB As New DBConnect
    Dim userID As New user
    Dim sql As String
    Dim gameID As Integer
    Dim gameName As String
    Dim participantID As Integer
    Dim groupID As Integer
    Dim ds As New DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        objDB = Session("db")
        userID = Session("user")

        lblName.Text = userID.firstname
        btnAdd.Visible = True
        btnSubmit.Visible = True

        If Not IsPostBack Then
            btnUpdate.Visible = False
            btnAdd.Visible = False
            btnSubmit.Visible = False

            sql = "SELECT Game.GameID, Game.GameName FROM GameParticipant INNER JOIN Game ON GameParticipant.GameID = Game.GameID WHERE GameParticipant.GroupID = 1 AND GameParticipant.ParticipantID ='" & userID.participantID & "'"
            ds = objDB.getDataSet(sql)
            GamesOwnedDropdownList.DataSource = ds
            GamesOwnedDropdownList.DataTextField = "GameName"
            GamesOwnedDropdownList.DataValueField = "GameID"
            GamesOwnedDropdownList.DataBind()

        End If

    End Sub

    Protected Sub GamesOwnedDropdownList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GamesOwnedDropdownList.SelectedIndexChanged

        gameID = GamesOwnedDropdownList.SelectedValue
        gameName = GamesOwnedDropdownList.SelectedItem.Text
        Dim gameParticipantID As Integer
        sql = "SELECT GroupID, Participant.ParticipantID, Participant.LastName, Participant.FirstName From Participant INNER JOIN GameParticipant ON Participant.ParticipantID = GameParticipant.ParticipantID WHERE (GameParticipant.GameID = '" & gameID & "')"
        ds = objDB.getDataSet(sql)
        gvPermissions.DataSource = ds
        gvPermissions.DataBind()

        'Dropdown list values
        For i As Integer = 0 To (gvPermissions.Rows.Count - 1)
            Dim ddl As DropDownList = CType(gvPermissions.Rows(i).Cells(3).FindControl("ddlPermissions"), DropDownList)
            gameParticipantID = gvPermissions.Rows(i).Cells(1).Text()

            sql = "SELECT EduGameGroup.GroupID, EduGameGroup.GroupName FROM EduGameGroup INNER JOIN GameParticipant on GameParticipant.GroupID = EduGameGroup.GroupID WHERE GameParticipant.ParticipantID = (SELECT Participant.ParticipantID FROM Participant WHERE Participant.ParticipantID = '" & gameParticipantID & "')"

            ds = objDB.getDataSet(sql)
            ddl.DataSource = ds
            ddl.DataTextField = "GroupName"
            ddl.DataValueField = "GroupID"
            ddl.DataBind()
        Next




    End Sub

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

        gameID = GamesOwnedDropdownList.SelectedValue
        Dim chkCheckBox As Boolean = validateChk()
        Dim gameParticipantID As Integer

        For i As Integer = 0 To (gvPermissions.Rows.Count - 1)
            Dim ddl As DropDownList = CType(gvAdd.Rows(i).Cells(2).FindControl("ddlAddPermissions"), DropDownList)

            If chkCheckBox = True Then
                Dim eduGameGroupID As Integer = ddl.SelectedValue
                gameParticipantID = gvAdd.Rows(i).Cells(1).Text

                sql = "UPDATE GameParticipant (GameID, ParticipantID, GroupID) VALUES ('" & gameID & "','" & gameParticipantID & "','" & eduGameGroupID & "')"
                objDB.doUpdate(sql)

            End If

        Next

    End Sub

    Protected Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        btnAdd.Visible = False
        btnUpdate.Visible = True
        gameID = GamesOwnedDropdownList.SelectedValue
        gameName = GamesOwnedDropdownList.SelectedItem.Text
        sql = "SELECT ParticipantID, LastName, FirstName FROM Participant WHERE LastName NOT IN (SELECT Participant.LastName FROM Participant INNER JOIN GameParticipant ON Participant.ParticipantID = GameParticipant.ParticipantID WHERE GameParticipant.GameID = '" & gameID & "')"
        ds = objDB.getDataSet(sql)
        gvAdd.DataSource = ds
        gvAdd.DataBind()

        For i As Integer = 0 To (gvAdd.Rows.Count - 1)

            Dim ddl As DropDownList = CType(gvAdd.Rows(i).Cells(2).FindControl("ddlAddPermissions"), DropDownList)
            sql = "SELECT GroupID, GroupName FROM EduGameGroup WHERE GroupTypeID = '" & 1 & "'"
            ds = objDB.getDataSet(sql)
            ddl.DataSource = ds
            ddl.DataTextField = "GroupName"
            ddl.DataValueField = "GroupID"
            ddl.DataBind()
        Next

    End Sub

    Protected Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click

        objDB = Session("db")
        gameID = GamesOwnedDropdownList.SelectedValue
        Dim chkCheckBox As Boolean = validateChk()
        Dim gameParticipantID As Integer
        Dim checkBox As CheckBox
        Dim ddl As DropDownList

        For i As Integer = 0 To (gvAdd.Rows.Count - 1)

            ddl = CType(gvAdd.Rows(i).FindControl("ddlAddPermissions"), DropDownList)
            checkBox = CType(gvAdd.Rows(i).FindControl("chAddPermissions"), CheckBox)

            If chkCheckBox = True AndAlso checkBox.Checked = True Then
                Dim eduGameGroupID As Integer = ddl.SelectedValue
                gameParticipantID = CInt(gvAdd.Rows(i).Cells(1).Text)

                sql = "INSERT INTO GameParticipant (GameID, ParticipantID, GroupID) VALUES ('" & gameID & "','" & gameParticipantID & "','" & eduGameGroupID & "')"
                objDB.doUpdate(sql)

                updatePermissions()
            End If

            Label1.Text = i

        Next
    End Sub

    Public Function updatePermissions()

        Dim gameParticipantID As Integer
        gameID = GamesOwnedDropdownList.SelectedValue
        gameName = GamesOwnedDropdownList.SelectedItem.Text

        'adding people to permissions table
        Dim sql2 As String = "SELECT Participant.ParticipantID, Participant.LastName, Participant.FirstName From Participant INNER JOIN GameParticipant ON Participant.ParticipantID = GameParticipant.ParticipantID WHERE (GameParticipant.GameID = '" & gameID & "')"
        ds = objDB.getDataSet(sql2)
        gvPermissions.DataSource = ds
        gvPermissions.DataBind()

        'Dropdown list values
        For i As Integer = 0 To (gvPermissions.Rows.Count - 1)
            Dim ddl As DropDownList = CType(gvPermissions.Rows(i).Cells(2).FindControl("ddlPermissions"), DropDownList)

            gameParticipantID = gvPermissions.Rows(i).Cells(1).Text()

            sql = "SELECT EduGameGroup.GroupID, EduGameGroup.GroupName FROM EduGameGroup INNER JOIN GameParticipant on GameParticipant.GroupID = EduGameGroup.GroupID WHERE GameParticipant.ParticipantID = (SELECT Participant.ParticipantID FROM Participant WHERE Participant.ParticipantID = '" & gameParticipantID & "')"

            ds = objDB.getDataSet(sql)
            ddl.DataSource = ds
            ddl.DataTextField = "GroupName"
            ddl.DataValueField = "GroupID"
            ddl.DataBind()
        Next

        'removing people who have been added to permissions table
        Dim sql3 As String = "SELECT ParticipantID, LastName, FirstName FROM Participant WHERE LastName NOT IN (SELECT Participant.LastName FROM Participant INNER JOIN GameParticipant ON Participant.ParticipantID = GameParticipant.ParticipantID WHERE GameParticipant.GameID = '" & gameID & "')"
        ds = objDB.getDataSet(sql3)
        gvAdd.DataSource = ds
        gvAdd.DataBind()


    End Function

    Public Function validateChk() As Boolean
        Dim i As Integer
        Dim chkBox As CheckBox
        'Checks to see if the user has selected a type of pizza
        For i = 0 To gvAdd.Rows.Count - 1
            chkBox = CType(gvAdd.Rows(i).Cells(0).FindControl("chAddPermissions"), CheckBox)
            If chkBox.Checked Then
                validateChk = True

                Exit For
            Else
                validateChk = False
            End If

            If validateChk = False Then
            End If
        Next
        Return validateChk
    End Function

    Protected Sub btnMainMenu_Click(sender As Object, e As EventArgs) Handles btnMainMenu.Click
        Response.Redirect("MainMenu.aspx")
    End Sub

    Private Sub gvAdd_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvAdd.RowCommand
        If e.CommandName.CompareTo("AddParticipant") = 0 Then
            objDB = Session("db")
            gameID = GamesOwnedDropdownList.SelectedValue
            Dim index = CInt(e.CommandArgument.ToString())
            Dim gameParticipantID As Integer
            Dim ddl As DropDownList

            ddl = CType(gvAdd.Rows(index).FindControl("ddlAddPermissions"), DropDownList)

            Dim eduGameGroupID As Integer = ddl.SelectedValue
            gameParticipantID = CInt(gvAdd.DataKeys(index).Value)

            sql = "INSERT INTO GameParticipant (GameID, ParticipantID, GroupID) VALUES ('" & gameID & "','" & gameParticipantID & "','" & eduGameGroupID & "')"
            objDB.doUpdate(sql)

            updatePermissions()

            Label1.Text = gameParticipantID & " " & gameID & " " & eduGameGroupID
        End If

    End Sub

    Private Sub gvPermissions_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvPermissions.RowCommand
        If e.CommandName.CompareTo("BtnAddParticipant") = 0 Then
            objDB = Session("db")
            gameID = GamesOwnedDropdownList.SelectedValue
            Dim index = CInt(e.CommandArgument.ToString())
            Dim gameParticipantID As Integer
            Dim ddl As DropDownList

            ddl = CType(gvAdd.Rows(index).FindControl("ddlPermissions"), DropDownList)

            Dim eduGameGroupID As Integer = ddl.SelectedValue
            gameParticipantID = CInt(gvAdd.DataKeys(index).Value)

            sql = "UPDATE GameParticipant (GameID, ParticipantID, GroupID) VALUES ('" & gameID & "','" & gameParticipantID & "','" & eduGameGroupID & "')"
            objDB.doUpdate(sql)

            updatePermissions()

            Label1.Text = gameParticipantID & " " & gameID & " " & eduGameGroupID
        End If

    End Sub
End Class